package com.dragon.english_practice_back.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.english_practice_back.entity.Message;
import com.dragon.english_practice_back.entity.Session;
import com.dragon.english_practice_back.mapper.SessionMapper;
import com.dragon.english_practice_back.service.MessageService;
import com.dragon.english_practice_back.mapper.MessageMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
* @author 伫倚
* @description 针对表【message(消息表)】的数据库操作Service实现
* @createDate 2025-03-09 20:14:01
*/
@Service
@AllArgsConstructor
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    SessionMapper sessionMapper;
    MessageMapper messageMapper;

    @Override
    public boolean hasAccessToSession(int id, Long sessionId) {
        // 获取会话信息
        Session session = sessionMapper.selectById(sessionId);

        // 如果会话不存在，返回false
        if (session == null) {
            return false;
        }

        // 检查用户是否是会话的创建者
        if (session.getCreatorId().equals(id)) {
            return true;
        }

        return false;
    }

    @Override
    public String getLastAssistantMessage(Integer userId, Integer sessionId) {
        List<Message> messages = messageMapper.getLastAssistantMessage(userId, sessionId);
        if (!messages.isEmpty()) {
            return messages.get(0).getContent();
        }
        return null;
    }

    @Override
    public boolean insertAssistantMessage(Integer userId, String content, Integer sessionId) {
        if (StrUtil.isEmpty(content)){
            log.error("content is none: {}", content);
            return false;
        }
        if (sessionMapper.selectById(sessionId) == null){
            log.error("sessionId is not exist : {}", sessionId);
            return false;
        }
        Message message = new Message();
        message.setType("assistant");
        message.setCreatorId(userId);
        message.setContent(content);
        message.setSessionId(sessionId);

        // 获取当前系统时间，精确到6位微秒
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
        String formattedDateTime = now.format(formatter);

        // 将格式化的时间字符串转换为Date对象
        Date currentDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
            currentDate = sdf.parse(formattedDateTime);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        message.setCreateDate(currentDate);
        if (messageMapper.insert(message) > 0) {
            log.debug("为sessionId：{}, 生成初始assistant消息成功,content：{}", sessionId, message.getContent());
        }
        else {
            log.error("为sessionId：{}, 生成初始assistant消息失败,content：{}", sessionId, message.getContent());
            return false;
        }

        return true;
    }
}




